<?php


namespace app\admin\controller;



use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class studentController extends AdminBaseController
{
    //显示
    public  function index(){
        $school_id=input('school_id');
        $room_id=input('room_id');
        $room = db('room')->where('school_id',$school_id)->select();
        $name= input('name');
       // halt($room);
        //连接班级表
        $data=db('student')
            //重命名
            ->alias('stu')
            //查询搜索
            ->where(function (Query $query) use ($school_id,$room_id,$name){
                if ($school_id){
                    $query->where('school_id','=',$school_id);
                }
                if ($room_id){
                    $query->where('room_id','=',$room_id);
                }
                if ($name){
                    $query->where('student_name','like',"%$name%");
                }
            })
            //连接条件
            //班级表
            ->join( 'room r','stu.room_id = r.id','LEFT')
            //学校表
            ->join( 'school s','r.school_id = s.id','LEFT')
            //省
            ->join('area a','a.areaId = stu.province','LEFT')
            //市
            ->join('area b','b.areaId = stu.city','LEFT')
            //区县
            ->join('area c','c.areaId = stu.district','LEFT')
            //查询指定字段
            ->field('stu.*,s.school_name,r.room_name,a.areaName as a,b.areaName as b,c.areaName as c')
            //通过id倒序
            ->order('id desc')
            //每页显示3条
            ->paginate(3);
//halt($data);

        //分页
        $page= $data ->render();
        //搜索学校
        $school=db('school')->select();
        //渲染模板
        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('school',$school);
        $this->assign('school_id',$school_id);
        $this->assign('room',$room);
        $this->assign('room_id',$room_id);
        //映射页面
        return $this->fetch();
    }

    //添加
    public function add(){

        //查询学校列表
        $school=db('school')->select();
        //查询所有省份
        $province =db('area')->where('level',1)->select();
        //渲染模板
        $this->assign('school',$school);
        $this->assign('province',$province);
        //映射页面
        return $this->fetch();
    }

    //将添加数据入库
    public function addpost(){
        //接收数据
        $data= input();
        //halt($data);
        //判断班级名称是否为空
        if (empty($data['student_name'])){
                $this->error('姓名不能为空');
        }
        if (empty($data['sex'])){
            $this->error('性别不能为空');
        }
        if (empty($data['age'])){
            $this->error('年龄不能空');
        }
        if (empty($data['idcard'])){
            $this->error('身份证号不能空');
        }

        //添加创建时间
        $data['create_time'] = date('Y-m-d H:m:s',time());

        //halt($data);

        unset($data['photo_name']);
        //添加到数据库
        $add = db('student')->insert($data);
        //判断是否成功
        if ($add){
            $this->success('添加成功','student/index');
        }else{
            $this->error('添加失败','student/index');
        }
    }

    //修改
    public function edit(){
        //接收id
        $id = input('id');
        //halt($id);
        //通过id查询该id的学生列表
        $stu=db('student')->get($id);
        //查询班级表

        $school_id= db('room')->where('id',$stu['room_id'])->value('school_id');
        $room=db('room')->where('school_id',$school_id)->select();
        //查询学校列表
        $school=db('school')->all();
       // halt($school);
        $province=db('area')->where('level',1)->all();
        $city=db('area')->where('level',2)->where('parentId',$stu['province'])->all();
        //halt($city);
        $district=db('area')->where('level',3)->where('parentId',$stu['city'])->all();
        //渲染模板
        $this->assign('stu',$stu);
        $this->assign('school',$school);
        $this->assign('room',$room);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('district',$district);
        $this->assign('school_id',$school_id);
        //映射页面
        return $this->fetch();
    }
    //保存修改信息
    public function editpost(){
        //接收数据
        $data= input();
       // halt($data);
        //判断名称是否为空
        if (empty($data['student_name'])){
            $this->error('姓名不能为空');
        }
        if (empty($data['sex'])){
            $this->error('性别不能为空');
        }
        if (empty($data['age'])){
            $this->error('年龄不能空');
        }
        if (empty($data['idcard'])){
            $this->error('身份证号不能空');
        }
        unset($data['photo_name']);
        //添加创建时间
        $data['update_time'] = date('Y-m-d H:m:s',time());
        //添加到数据库
        //halt($data);
        $edit = db('student')->update($data);
        if ($edit){
            $this->success('修改成功','student/index');
        }else{
            $this->error('修改失败','student/index');
        }
    }

    //删除
    public function delete()
    {
        //查询要删除的id
        $id = $this->request->param('id', 0, 'intval');

        //通过id删除该条信息，并判断是否成功
        if (Db::name('student')->delete($id) !== false) {

            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    //查询该学校id下的所有班级
    public function getSchool(){
        $schoolId=input('school_id');
        //halt($schoolId);
        $room=db('room')
            ->where('school_id',$schoolId)->select();
       // halt($room);
        if ($room){
            $options=['<option value="">--请选择班级--</option>'];
            foreach ($room as $v){
                $opt="<option value='".$v['id']."'>".$v['room_name']."</option>";
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }

    //根据省id查询该省下所有市
    public function getProvince(){
        $areaId=input('areaId');
        //halt($schoolId);
        $city=db('area')
            ->where('parentId',$areaId)->select();
        if ($city){
            $options=['<option value="">--请选择市--</option>'];
            foreach ($city as $v){
                $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }

//根据市id查询该市下所有区县
    public function getCity(){
        $areaId=input('areaId');
        //halt($schoolId);
        $district=db('area')
            ->where('parentId',$areaId)->select();
        if ($district){
            $options=['<option value="">--请选择区县--</option>'];
            foreach ($district as $v){
                $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }
}